package com.hcy.app.screen.music.components

import android.os.Environment
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.hcy.app.domain.FileUploadServer
import com.hcy.app.utils.DeviceInfo
import java.io.File

@Composable
fun File_Upload(){
    val context = LocalContext.current
    var serverStatus by remember { mutableStateOf<Boolean>(false) }
    var serverAddress by remember { mutableStateOf("") }

    val uploadDir = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "music")
    if (!uploadDir.exists()) uploadDir.mkdirs()
    val port=9999
    val server = FileUploadServer( port, uploadDir.absolutePath).apply {
        start()
        serverStatus = true
        serverAddress = "http://${DeviceInfo.getIPAddress(context)}:$port"
    }

    DisposableEffect(Unit){
        onDispose {
            // 当 Composable 离开组合时执行
            if (serverStatus){
                server.stop()
                serverStatus = false
                serverAddress = ""
            }
        }
    }
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Button(
            onClick = {
                if (!serverStatus) {
                    serverStatus = true
                    serverAddress = "http://${DeviceInfo.getIPAddress(context)}:$port"
                } else {
                    // 停止服务器逻辑可以在这里添加
                    serverStatus = false
                    serverAddress = ""
                }
            },
            colors = ButtonDefaults.buttonColors(
                containerColor = if (serverStatus) MaterialTheme.colorScheme.errorContainer
                else MaterialTheme.colorScheme.primaryContainer
            )
        ) {
            Text(if (serverStatus) "停止HTTP服务" else "启动HTTP服务")
        }

        Spacer(modifier = Modifier.height(16.dp))

        if (serverStatus) {
            Text(
                text = "服务运行中",
                color = MaterialTheme.colorScheme.primary
            )
            Spacer(modifier = Modifier.height(8.dp))
            Text(
                text = "访问地址: $serverAddress",
                color = MaterialTheme.colorScheme.secondary
            )
            Spacer(modifier = Modifier.height(8.dp))
            Text(
                text = "在同一网络下的设备可以通过浏览器访问此地址上传文件",
                color = MaterialTheme.colorScheme.onSurfaceVariant
            )
        } else {
            Text(
                text = "服务已停止",
                color = MaterialTheme.colorScheme.error
            )
        }
    }
}